package com.shiikong_memory.yez.Arknights;

import com.alibaba.fastjson2.JSONArray;
import com.shiikong_memory.yez.Arknights.ArknightsData.AgentList;
import com.shiikong_memory.yez.Arknights.DataEnum.RoguelikeEnum;
import com.shiikong_memory.yez.MySQLAPI.DaoImpl.AccountDataServer;
import com.shiikong_memory.yez.MySQLAPI.JavaBean.AccountDataBean;
import love.forte.simbot.component.kook.event.KookChannelMessageEvent;
import love.forte.simbot.kook.api.message.SendChannelMessageApi;
import love.forte.simbot.quantcat.common.annotations.ContentTrim;
import love.forte.simbot.quantcat.common.annotations.Listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CompletableFuture;

@Component
public class IntensiveStrategyRandom {
    @Autowired
    AccountDataServer accountDataServer;

    @Autowired
    SklandAPI sklandAPI;

    private static Random random = new Random();

    private static Map<String, AgentList> cache = new HashMap<>();

    @Listener
    @ContentTrim
    public CompletableFuture<?> login(KookChannelMessageEvent event){
        int i=-1;
        if (event.getSourceEvent().getContent().matches("^肉鸽随机开局$")){
            i=0;
        }else if (event.getSourceEvent().getContent().matches("^肉鸽随机开局 简单$")){
            i=3;
        }else if (event.getSourceEvent().getContent().matches("^肉鸽随机开局 普通$")){
            i=6;
        }else if (event.getSourceEvent().getContent().matches("^肉鸽随机开局 困难$")){
            i=10;
        }else if (event.getSourceEvent().getContent().matches("^肉鸽随机开局 地狱$")){
            i = 30;
        }
        if (i>=0){
            String kookId = event.getAuthor().getId().toString();
            AgentList agentList;
            if (cache.containsKey(kookId)){
                agentList = cache.get(kookId);
            }else {
                AccountDataBean dataBean = accountDataServer.getByKookId(kookId);
                if (dataBean == null){
                    return event.replyAsync("未获取到游戏数据，请先获取明日方舟数据");
                }
                agentList = getEvolvePhase2List(dataBean);
                cache.put(kookId, agentList);
            }
            RoguelikeEnum roguelikeEnum = RoguelikeEnum.Random();
            AgentList.Agent randomAgent = agentList.getRandomAgent();
            if (i==0) event.getContent().sendAsync(SendChannelMessageApi.create(10,event.getSourceEvent().getTargetId(), Json.formatted(roguelikeEnum, roguelikeEnum.getRandomName(), String.valueOf(random.nextInt(15)+1), randomAgent.getName()+"<"+randomAgent.getOccupation().toString()+">")));
            else {
                StringBuffer data = new StringBuffer();
                List<AgentList.Agent> noRandomAgent = agentList.getNoRandomAgent(i, randomAgent);
                if (noRandomAgent == null){
                    return event.replyAsync("干员精二数量不足，无法使用该难度，该难度需要%d个精二干员".formatted(i));
                }
                for (AgentList.Agent agent:noRandomAgent){
                    data.append(agent.getName()).append("<").append(agent.getOccupation().toString()).append(">").append("   ");
                }
                return event.getContent().sendAsync(SendChannelMessageApi.create(10,event.getSourceEvent().getTargetId(), Json2.formatted(roguelikeEnum, roguelikeEnum.getRandomName(), String.valueOf(random.nextInt(15)+1), randomAgent.getName()+"<"+randomAgent.getOccupation().toString()+">", data)));
            }
        }
        return null;
    }

    public AgentList getEvolvePhase2List(AccountDataBean accountDataBean){
        return new AgentList(JSONArray.parseArray(accountDataBean.getChars()), JSONArray.parseArray(accountDataBean.getCharInfoMap()));
    }

    @Async
    @Scheduled(cron="0 0 0 * * ?")
    public void clearCache(){
        cache.clear();
    }

    public static void clearCache(String kookId){
        cache.remove(kookId);
    }

    private String Json = """
            [
              {
                "type": "card",
                "theme": "secondary",
                "size": "lg",
                "modules": [
                  {
                    "type": "header",
                    "text": {
                      "type": "plain-text",
                      "content": "今日随机肉鸽开局"
                    }
                  },
                  {
                    "type": "divider"
                  },
                  {
                    "type": "section",
                    "text": {
                      "type": "paragraph",
                      "cols": 3,
                      "fields": [
                        {
                          "type": "kmarkdown",
                          "content": "**主题**\\n%s"
                        },
                        {
                          "type": "kmarkdown",
                          "content": "**分队**\\n%s"
                        },
                        {
                          "type": "kmarkdown",
                          "content": "**难度**\\n%s"
                        }
                      ]
                    }
                  },
                  {
                    "type": "section",
                    "text": {
                      "type": "kmarkdown",
                      "content": "**开局干员：%s**"
                    }
                  },
                  {
                    "type": "divider"
                  }
                ]
              }
            ]
            """;

    private String Json2 = """
            [
              {
                "type": "card",
                "theme": "secondary",
                "size": "lg",
                "modules": [
                  {
                    "type": "header",
                    "text": {
                      "type": "plain-text",
                      "content": "今日随机肉鸽开局"
                    }
                  },
                  {
                    "type": "divider"
                  },
                  {
                    "type": "section",
                    "text": {
                      "type": "paragraph",
                      "cols": 3,
                      "fields": [
                        {
                          "type": "kmarkdown",
                          "content": "**主题**\\n%s"
                        },
                        {
                          "type": "kmarkdown",
                          "content": "**分队**\\n%s"
                        },
                        {
                          "type": "kmarkdown",
                          "content": "**难度**\\n%s"
                        }
                      ]
                    }
                  },
                  {
                    "type": "section",
                    "text": {
                      "type": "kmarkdown",
                      "content": "**开局干员：%s**"
                    }
                  },
                  {
                    "type": "section",
                    "text": {
                      "type": "kmarkdown",
                      "content": "**禁用干员：**"
                    }
                  },
                  {
                    "type": "section",
                    "text": {
                      "type": "plain-text",
                      "content": "%s"
                    }
                  },
                  {
                    "type": "divider"
                  }
                ]
              }
            ]
            """;
}
